<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>后台管理系统</title>
    <!-- Tell the browser to be responsive to screen width -->
    <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
    <!-- Bootstrap 3.3.6 -->
    <link rel="stylesheet" href="bootstrap/css/bootstrap.min.css">
    <!-- Font Awesome -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.5.0/css/font-awesome.min.css">
    <!-- Theme style -->
    <link rel="stylesheet" href="dist/css/AdminLTE.min.css">
    <!-- AdminLTE Skins. We have chosen the skin-blue for this starter
    page. However, you can choose any other skin. Make sure you
    apply the skin class to the body tag so the changes take effect.
    -->
    <link rel="stylesheet" href="dist/css/skins/skin-blue.min.css">

    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>
<body class="hold-transition skin-blue sidebar-mini">
<div class="wrapper">
<#include "header.ftl" />
<#include "sidebar.ftl" />
    <!-- Content Wrapper. Contains page content -->
    <div class="content-wrapper" id="app">
        <!-- Content Header (Page header) -->
        <section class="content-header">
            <h1>
                内容管理
                <small>文章管理</small>
            </h1>
            <ol class="breadcrumb">
                <li><a href="#"><i class="fa fa-dashboard"></i> 内容管理</a></li>
                <li class="active">文章管理</li>
            </ol>
        </section>

        <!-- Main content -->
        <section class="content">
            <div class="box">
                <div class="box-header">
                <@shiro.hasPermission name="system:sys_role:add">
                    <button type="button" class="btn btn-info" @click="addContent()"><i class="fa fa-user-plus"></i> 新增文章
                    </button>
                </@shiro.hasPermission>
                </div>
                <div class="box-body table-responsive no-padding">
                    <table class="table table-hover">
                        <tr>
                            <th>ID</th>
                            <th>作者</th>
                            <th>标题</th>
                            <th>导语</th>
                            <th>图片地址</th>
                            <th>文章地址</th>
                            <th>类型</th>
                            <th>提交日期</th>
                            <th>操作</th>
                        </tr>
                        <tr v-for="content in contents">
                            <td>{{ content.id }}</td>
                            <td>{{ content.author }}</td>
                            <td>{{ content.title }}</td>
                            <td>{{ content.lead }}</td>
                            <td>{{ content.photoUrl }}</td>
                            <td>{{ content.docUrl }}</td>
                            <td>{{ content.type }}</td>
                            <td>{{ content.submitDate }}</td>
                            <td>
                            <@shiro.hasPermission name="system:sys_role:del">
                                <button type="button" class="btn btn-danger"
                                        @click="delContent(content.id)">
                                    删除
                                </button>
                            </@shiro.hasPermission>
                            </td>
                        </tr>
                    </table>
                <@shiro.hasPermission name="system:sys_role:add">
                    <modal :show.sync="showContentModal" title="新增文章" effect="fade">
                        <div slot="modal-body" class="modal-body">
                            <form class="form-horizontal">
                                <div class="form-group">
                                    <label for="inputTitle" class="col-sm-2 control-label">标题</label>

                                    <div class="col-sm-10">
                                        <input type="text" class="form-control" id="inputTitle" v-model="content.title"
                                               placeholder="标题">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label for="inputLead" class="col-sm-2 control-label">导语</label>

                                    <div class="col-sm-10">
                                        <input type="text" class="form-control" id="inputLead" v-model="content.lead"
                                               placeholder="导语">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label for="inputPhotoUrl" class="col-sm-2 control-label">图片地址</label>

                                    <div class="col-sm-10">
                                        <input type="text" class="form-control" id="inputPhotoUrl" v-model="content.photoUrl"
                                               placeholder="图片地址">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label for="inputDocUrl" class="col-sm-2 control-label">文章地址</label>

                                    <div class="col-sm-10">
                                        <input type="text" class="form-control" id="inputDocUrl"
                                               v-model="content.docUrl" placeholder="文章地址">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label for="inputDescription" class="col-sm-2 control-label">类型</label>

                                    <div class="col-sm-10">
                                        <select v-model="content.type">
                                            <option value="CardInfo" selected>喵喵卡讯</option>
                                            <option value="BackGarden">喵喵后花园</option>
                                        </select>
                                    </div>
                                </div>
                            </form>
                        </div>
                        <div slot="modal-footer" class="modal-footer">
                            <button type="button" class="btn btn-default" @click='closeContentModal()'>关闭</button>
                            <button type="button" class="btn btn-primary" @click='doAddContent()'>保存</button>
                        </div>
                    </modal>
                </@shiro.hasPermission>
                </div>
                <!-- /.box-body -->
                <div class="box-footer clearfix">
                    <ul class="pagination pagination-sm no-margin pull-right">
                        <li class="first" v-bind:class="{'disabled': first}">
                            <a v-if="first">首页</a>
                            <a v-else @click="toFirst()">首页</a>
                        </li>
                        <li class="prev" v-bind:class="{'disabled': first}">
                            <a v-if="first">上一页</a>
                            <a v-else @click="toPrev()">上一页</a>
                        </li>
                        <template v-for="index in totalPages">
                            <li class="paginate_button {{classRenderer(index)}}">
                                <a @click="jumpTo(index)">{{ index + 1 }}</a>
                            </li>
                        </template>
                        <li class="next" v-bind:class="{'disabled': last}">
                            <a v-if="last">下一页</i></a>
                            <a v-else @click="toNext()">下一页</i></a>
                        </li>
                        <li class="last" v-bind:class="{'disabled': last}">
                            <a v-if="last">末页</a>
                            <a v-else @click="toLast()">末页</a>
                        </li>
                    </ul>
                </div>
            </div>
            <!-- /.box -->
        </section>
        <!-- /.content -->
    </div>
    <!-- /.content-wrapper -->

<#include "footer.ftl" />
</div>
<!-- ./wrapper -->

<!-- REQUIRED JS SCRIPTS -->

<!-- jQuery 2.2.0 -->
<script src="plugins/jQuery/jQuery-2.2.0.min.js"></script>
<!-- Bootstrap 3.3.6 -->
<script src="bootstrap/js/bootstrap.min.js"></script>
<!-- AdminLTE App -->
<script src="dist/js/app.min.js"></script>
<#--Vue.js-->
<script src="vue/vue.min.js"></script>
<script src="vue/vue-resource.min.js"></script>
<script src="vue/vue-strap.min.js"></script>
<script>
    var modal = VueStrap.modal;
    new Vue({
        el: '#app',
        data: {
            contents: [],
            content: {},
            showContentModal: false,
            totalPages: "",
            totalElements: "",
            last: "",
            size: "",
            number: 0,
            sort: [],
            first: "",
            numberOfElements: ""
        },
        components: {
            modal
        },
        ready: function () {
            this.$http.get("/contents/list", function (data) {
                this.contents = data.content;
                this.first = data.first;
                this.last = data.last;
                this.totalPages = data.totalPages;
                this.number = data.number;
            })
        },
        methods: {
            classRenderer: function (index) {
                if (index == this.number) {
                    return 'active';
                }
                return '';
            },
            toFirst: function () {
                this.getList(0);
            },
            toLast: function () {
                this.getList(this.totalPages - 1);
            },
            toPrev: function () {
                this.getList(this.number - 1);
            },
            toNext: function () {
                this.getList(this.number + 1);
            },
            jumpTo: function (index) {
                this.getList(index);
            },
            getList: function (page) {
                this.$http.get("/contents/list", {page: page}).then(function (response) {
                    var data = response.data;
                    this.contents = data.content;
                    this.first = data.first;
                    this.last = data.last;
                    this.totalPages = data.totalPages;
                    this.number = data.number;
                }, function (data) {
                    console.log(data);
                })
            },
            addContent: function () {
                this.showContentModal = true;
            },
            doAddContent: function () {
                var content = {
                    title: this.content.title,
                    lead: this.content.lead,
                    photoUrl: this.content.photoUrl,
                    docUrl: this.content.docUrl,
                    type: this.content.type
                };
                this.$http.post("/contents", content).then(function (data) {
                    this.getList(this.number);
                    this.showContentModal = false;
                });
                this.content.title = null;
                this.content.lead = null;
                this.content.photoUrl = null;
                this.content.docUrl = null;
            },
            delContent: function(contentId) {
                this.$http.delete("/contents/" + contentId).then(function(data) {
                    this.getList(0);
                })
            },
            closeContentModal: function () {
                this.content.title = null;
                this.content.lead = null;
                this.content.photoUrl = null;
                this.content.docUrl = null;
                this.showContentModal = false;
            }
        }
    })
</script>

</body>
</html>